'use strict';

const xtpl = require('xtpl');
const path = require('path');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));
const fs = require('fs');


module.exports.projectList = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 3;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('project').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('project').alias('project').join({
            table: 'school',
            join: 'left',
            as: 'school',
            on: ['school_id', 'school_id']
        }).join({
            table: 'pro_type',
            join: 'left',
            as: 'pro_type',
            on: ['pro_type_id', 'pro_type_id']
        }).where(where).order('project.pro_id DESC').limit(start,limit).select().then(function(project_list){

            let data = {
                array:project_list,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"projectList.html",data);
        });


    });
};


module.exports.projectAdd = function(req,res)
{
    db.table('project').select().then(function(res_pro){
        db.table('school').select().then(function(res_school){
            db.table('pro_type').select().then(function(pro_type){
                let Data = {
                "project_list":res_pro,
                "school_list":res_school,
                "pro_type_list":pro_type
                };
                CommonHelper.renderFile(req,res,"projectAdd.html",Data);
            })
        })
    });
};

module.exports.projectAddForm = function(req,res)
{
    //先获取表单的数据

    //先检测一下 该用户是否存在
    let pro_name = req.body.pro_name;
    let school_id = req.body.school_id;
    let pro_type_id = req.body.pro_type_id;
    db.table('project').where(`pro_name = '${pro_name}' AND school_id = '${school_id}' AND pro_type_id = '${pro_type_id}'`).find().then(function(data){
        if(JSON.stringify(data) != "{}")
        {
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end("<script>alert('该学校课程已存在');location.href='/project/projectAdd';</script>");
        }else{
            let data = {
                "pro_name":req.body.pro_name,
                "school_id":req.body.school_id,
                "pro_type_id":req.body.pro_type_id
            }
            db.table('project').add(data).then(function(projecttId){
                if(projecttId)
            {
                //重定向
                res.redirect(`/project/projectList`);
                return false;
            }else{
                //重定向
                res.redirect(`/project/projectAdd`);
                return false;
            }
            })
        }
    });

};

module.exports.projectEdit = function(req,res)
{
    let pro_id = req.query.pro_id;

    if(!pro_id)
    {
        //重定向
        res.redirect(`/project/projectList`);
        return false;
    }


    let where = 1;
    db.table('project').where(`pro_id = ${pro_id}`).find().then(function(pro_Id){
        db.table('school').where(where).select().then(function(affectschoolId){
            db.table('pro_type').where(where).select().then(function(typeId){
                let data = {
                    "project":pro_Id,
                    "school_list":affectschoolId,
                    "pro_type":typeId
                }
                CommonHelper.renderFile(req,res,"projectEdit.html",data);
            })
        })

    })

};

module.exports.projectEditForm = function(req,res)
{
    let pro_id = req.query.pro_id;

    if(!pro_id)
    {
        //重定向
        res.redirect(`/project/projectList`);
        return false;
    }

    db.table('project').where(`pro_id = ${pro_id}`).find().then(function(projectId){

        let data = {
            "pro_name":req.body.pro_name,
            "school_id":req.body.school_id,
            "pro_type_id":req.body.pro_type_id
        }
        db.table('project').where(`pro_id = ${pro_id}`).update(data).then(function(affectId){

            if(affectId)
            {
                //重定向
                res.redirect(`/project/projectList`);
                return false;
            }else{
                //重定向
                res.redirect(`/project/projectEdit?pro_id=${pro_id}`);
                return false;
            }
        });

    });
};

module.exports.projectDelete = function(req,res)
{
    let delete_id = req.query.delete_id

    db.table('project').where(`pro_id = ${delete_id}`).delete().then(function(res_pro){
        if(res_pro)
        {
            res.redirect(`/project/projectList`);
            return false;
        }else{
           res.redirect(`/project/projectList`);
            return false;
        }
    })
}